using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using DTO;
using BLL;

namespace WellmeadowsHospital
{
    public partial class frmUserManagement : DevExpress.XtraEditors.XtraUserControl
    {
        StaffBLL staffBLL = new StaffBLL();
        UserBLL userBLL = new UserBLL();

        public frmUserManagement()
        {
            //txtUserStaffIDT.Enabled = false;
            //txtUserNameT.Enabled = false;
            InitializeComponent();
        }

        private void txtUserSearchStaff_KeyPress(object sender, KeyPressEventArgs e)
        {
            gcUserStaffList.DataSource = staffBLL.SearchIDNamePosition(txtUserSearchStaff.Text);
            if (gvUserStaffList.GetRowCellValue(gvUserStaffList.SelectedRowsCount - 1, gcUserStaffID) != null)
            {
                txtUserStaffIDT.Text = gvUserStaffList.GetRowCellValue(gvUserStaffList.SelectedRowsCount - 1, gcUserStaffID).ToString();
                txtUserNameT.Text = gvUserStaffList.GetRowCellValue(gvUserStaffList.SelectedRowsCount - 1, gcUserName).ToString();
            }
        }

        private void gvUserStaffList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtUserStaffIDT.Text = gvUserStaffList.GetRowCellValue(gvUserStaffList.GetSelectedRows()[0], gcUserStaffID).ToString();
            txtUserNameT.Text = gvUserStaffList.GetRowCellValue(gvUserStaffList.GetSelectedRows()[0], gcUserName).ToString();
            //right click
        }

        private void btnUserSave_Click(object sender, EventArgs e)
        {
            if (txtUserPasswordT.Text.Equals(txtUserRetypePasswordT.Text))
            {
                UserDTO user = new UserDTO();
                user.StaffID = txtUserStaffIDT.Text;
                user.Password = txtUserPasswordT.Text;
                user.Role = cbbUserRoleT.SelectedItem.ToString();
                user.Status = cbbUserStatusT.SelectedItem.ToString();

                if (userBLL.Insert(user).Equals("success"))
                {
                    MessageBox.Show("Insert success");
                    gcUserList.DataSource = userBLL.Select();
                }
                else
                {
                    MessageBox.Show(userBLL.Insert(user));
                }
            }
            else
            {
                MessageBox.Show("password not match");
                gcUserList.DataSource = userBLL.ToString();
            }
        }

        private void gvUserList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            txtUserStaffIDT.Text = gvUserList.GetRowCellValue(gvUserList.GetSelectedRows()[0], gcUserListStaffID).ToString();
            txtUserNameT.Text = gvUserList.GetRowCellValue(gvUserList.GetSelectedRows()[0], gcUserListName).ToString();
       
        }
    }
}
